package chapter07

object Test01_immutableArray {
  def main(args: Array[String]): Unit = {
    //1创建数组
    val arr: Array[Int] = new Array[Int](5)
    //另一种创建方法
    val arr2 = Array(12, 37, 56, 48, 65)
    //2.访问元素
    println(arr2(2))
    println(arr2.mkString(","))
    //3.数组的遍历
    // 1)普通for循环
    for (i <- 0 until arr2.length) println(arr2(i))
    println("=============================")
    // 2)
    for (i <- arr2.indices) println(arr2(i))
    println("=============================")
    //3)增强for循环
    for (elem <- arr2) println(elem)
    println("=============================")
    //4)迭代器
    val iter = arr2.iterator
    while (iter.hasNext) println(iter.next())
    println("=============================")
    //5)foreach 传入一个匿名函数
    arr2.foreach((elem: Int) => println(elem))
    println("=============================")
    //4.添加元素
    val newArr = arr2.:+(73)
    println(newArr.mkString(","))
    val newArr2 = arr2.+:(30)
    println(newArr2.mkString(","))
    val newArr3 = newArr :+ 15
    println(newArr3.mkString(","))
    val newArr4 = 29 +: newArr
    println(newArr4.mkString(","))
  }
}
